﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.Web.Areas.SafetyConstruction.ViewModels;
using KingRoad.EPM.SafetyManagementEntity.Model;
using KingRoad.EPM.Web.Areas.SafetyConstruction.SafetyConstructionFramework.Common;
using KingRoad.EPM.SafetyManagementBLL.UIRules;
using Unis.Framework.Core.Infrastructure;
using System.Dynamic;

namespace KingRoad.EPM.Web.Areas.SafetyConstruction.Controllers
{
    public class ScmCostPlanController : BaseController
    {
        //
        // GET: /ScmCostPlan/
        /// <summary>
        /// 费用计划-列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CostPlanList(string pageName)
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            var CostPlanmainList = ScmCostplanmainUIRule.GetAllCostplanmainForPage(ViewBag.pageNum, ViewBag.numPerPage, "Cpm_OperateTime", out totalRecord, " Cpm_Section in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
           // return View(new ScmCostplanViewModel { TheCostPlanMainList = CostPlanmainList });
            return String.IsNullOrEmpty(pageName) ? View(new ScmCostplanViewModel { TheCostPlanMainList = CostPlanmainList }) : View(pageName, new ScmCostplanViewModel { TheCostPlanMainList = CostPlanmainList });

        }
        /// <summary>
        /// 费用计划-列表(分页提交)
        /// </summary>
        /// <returns></returns>
       
        public ActionResult SplitPage()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            if (ViewBag.pageNum == 0 || ViewBag.numPerPage == 0)
                return CostPlanList("CostPlanList");

            ViewBag.iput_biaoduan = Request.Form["iput_biaoduan"];
            ViewBag.iput_unit = Request.Form["iput_unit"];
            ViewBag.iput_timestart = Request.Form["iput_timestart"];
            ViewBag.iput_timeend = Request.Form["iput_timeend"];
            string sql = " Cpm_Section in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
            if (ViewBag.iput_biaoduan != "")
                sql += " and Cpm_Section like '%" + ViewBag.iput_biaoduan + "%' ";
            if (ViewBag.iput_unit != "")
                sql += " and Cpm_ConstructionName like '%" + ViewBag.iput_unit + "%' ";
            if (ViewBag.iput_timestart != "")
                sql += " and Cpm_YearMonth>='" + Convert.ToDateTime(ViewBag.iput_timestart) + "' ";
            if (ViewBag.iput_timeend != "")
                sql += " and Cpm_YearMonth<='" + Convert.ToDateTime(ViewBag.iput_timeend) + "' ";
            var CostPlanmainList = ScmCostplanmainUIRule.GetAllCostplanmainForPage(ViewBag.pageNum, ViewBag.numPerPage, "Cpm_OperateTime", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("CostPlanList", new ScmCostplanViewModel { TheCostPlanMainList = CostPlanmainList });
        }
        /// <summary>
        /// 费用计划-添加
        /// </summary>
        /// <returns></returns>
        public ActionResult CostPlanAdd()
        {
           
            return View();
        }
        /// <summary>
        /// 费用计划-添加 
        /// 功能：执行添加操作
        /// </summary>
        /// <returns></returns>
        public JsonResult CostPlanAddData(ScmCostplanViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheScmCostplanmainInfo.Cpm_ID = Guid.NewGuid().ToString();
                vm.TheScmCostplanmainInfo.Cpm_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheScmCostplanmainInfo.Cpm_OperateUserName = GetLoginUserRealName;
                vm.TheScmCostplanmainInfo.Cpm_OperateTime = DateTime.Now;
                vm.TheScmCostplanmainInfo.Cpm_State = 0;
                int costplan = ScmCostplanmainUIRule.AddCostplanmain(vm.TheScmCostplanmainInfo);
                foreach (var item in vm.TheCostPlanList)
                {
                    item.Cp_ID= Guid.NewGuid().ToString();
                    item.Cpm_ID = vm.TheScmCostplanmainInfo.Cpm_ID;
                    int cp = ScmCostplanmainUIRule.AddCostplan(item);
                }
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    navTabId = "SafetyConstruction.ScmCostPlan.CostPlanList",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 费用计划——编辑
        /// 功能：根据id加载相对应数据
        /// </summary>
        /// <returns></returns>
        public ActionResult CostPlanEdit(Guid id)
        {
            var CostPlanmainInfo = ScmCostplanmainUIRule.GetCostplanmainById(id);
            var CostPlanList = ScmCostplanmainUIRule.GetCostplanByFId(id);
            return View(new ScmCostplanViewModel { TheScmCostplanmainInfo = CostPlanmainInfo, TheCostPlanList = CostPlanList });
            //return View("SafeProductionAdd");
        }
        /// <summary>
        /// 费用计划-修改  
        /// 功能：执行修改保存操作
        /// </summary>
        /// <returns></returns>
        public JsonResult CostPlanEditData(ScmCostplanViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {

                vm.TheScmCostplanmainInfo.Cpm_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheScmCostplanmainInfo.Cpm_OperateUserName = GetLoginUserRealName;
                vm.TheScmCostplanmainInfo.Cpm_OperateTime = DateTime.Now;
                vm.TheScmCostplanmainInfo.Cpm_State = 0;
                int costplan = ScmCostplanmainUIRule.UpdateCostPlanmain(vm.TheScmCostplanmainInfo);
                int delcostplan = ScmCostplanmainUIRule.DeleteCostplanByFId(vm.TheScmCostplanmainInfo.Cpm_ID);
                if (vm.TheCostPlanList!=null)
                {
                  foreach (var item in vm.TheCostPlanList)
                {
                    item.Cp_ID = Guid.NewGuid().ToString();
                    item.Cpm_ID = vm.TheScmCostplanmainInfo.Cpm_ID;
                    int cp = ScmCostplanmainUIRule.AddCostplan(item);
                }
                }
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "更新成功",
                    navTabId = "SafetyConstruction.ScmCostPlan.CostPlanList",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "更新失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 费用计划-查看
        /// </summary>
        /// <returns></returns>
        public ActionResult CostPlanView(Guid id)
        {
            var TheScmCostplanmainInfo = ScmCostplanmainUIRule.GetCostplanmainById(id);
            var TheCostPlanList = ScmCostplanmainUIRule.GetCostplanByFId(id);
            var TheScmAuditList = ScmAuditUIRule.GetAuditListByPID(id.ToString());
            var TheScmAuditInfo = new ScmAuditInfo();
            if (TheScmAuditList.Count!=0)
            {
                TheScmAuditInfo = TheScmAuditList.FirstOrDefault();
            }
            return View(new ScmCostplanViewModel { TheScmCostplanmainInfo = TheScmCostplanmainInfo, TheCostPlanList = TheCostPlanList, TheScmAuditInfo=TheScmAuditInfo });
        }
        /// <summary>
        /// 费用计划-审批
        /// </summary>
        /// <returns></returns>
        public ActionResult CostPlanAudit(Guid id)
        {
            var CostPlanmainInfo = ScmCostplanmainUIRule.GetCostplanmainById(id);
            var CostPlanList = ScmCostplanmainUIRule.GetCostplanByFId(id);
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "审批通过", Value = "true" });
            list.Add(new SelectListItem { Text = "审批不通过", Value = "false" });
            ViewBag.auList = list;
            ScmAuditInfo ainfo = new ScmAuditInfo();
            ainfo.A_DepID = GetLoginUserDepartmentId;
            ainfo.A_DepName = GetLoginUserDepartmentName;
            ainfo.A_ManID = GetLoginUserId;
            ainfo.A_ManName = GetLoginUserRealName;
            ainfo.A_Time = DateTime.Now;
            
            return View(new ScmCostplanViewModel { TheScmCostplanmainInfo = CostPlanmainInfo, TheCostPlanList = CostPlanList,TheScmAuditInfo=ainfo });
        }
        /// <summary>
        /// 费用计划-审批  
        /// 功能：执行审批保存操作
        /// </summary>
        /// <returns></returns>
        public JsonResult CostPlanAuditData(ScmCostplanViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (vm.TheScmAuditInfo.A_IsApproved == true)
                {
                    vm.TheScmAuditInfo.A_State = 1;
                    vm.TheScmCostplanmainInfo.Cpm_State = 1;
                }
                else
                {
                    vm.TheScmAuditInfo.A_State = 2;
                    vm.TheScmCostplanmainInfo.Cpm_State = 2;
                }
                int costplanmain = ScmCostplanmainUIRule.UpdateCostPlanmain(vm.TheScmCostplanmainInfo);
                
                vm.TheScmAuditInfo.A_ID = Guid.NewGuid().ToString();
                vm.TheScmAuditInfo.A_PrimayID = vm.TheScmCostplanmainInfo.Cpm_ID;
                vm.TheScmAuditInfo.A_DepID = GetLoginUserDepartmentId;
                vm.TheScmAuditInfo.A_DepName = GetLoginUserDepartmentName;
                ScmAuditUIRule.AddAudit(vm.TheScmAuditInfo,3,2);
                int delcostplan = ScmCostplanmainUIRule.DeleteCostplanByFId(vm.TheScmCostplanmainInfo.Cpm_ID);
                if (vm.TheCostPlanList != null)
                {
                    foreach (var item in vm.TheCostPlanList)
                    {
                        item.Cp_ID = Guid.NewGuid().ToString();
                        item.Cpm_ID = vm.TheScmCostplanmainInfo.Cpm_ID;
                        int cp = ScmCostplanmainUIRule.AddCostplan(item);
                    }
                }
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "审批成功",
                    navTabId = "SafetyConstruction.ScmCostPlan.CostPlanList",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "审批失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 费用计划-删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult CostPlanDelete(string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                int safe = ScmCostplanmainUIRule.DeleteCostplanmain(id);
            });

            dynamic jsonReturn = new ExpandoObject();
            //删除成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    navTabId = "SafetyConstruction.ScmCostPlan.CostPlanList",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 费用计划-批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public JsonResult CostPlanMultiDelete()
        {
            dynamic jsonReturn = new ExpandoObject();
            var ids = base.GetRequestFormValue<string>("ids");
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                int safe = ScmCostplanmainUIRule.DeleteCostPlanmainMulti(ids);
            });

            //保存成功
            if (true)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    navTabId = "SafetyConstruction.ScmCostPlan.CostPlanList",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
    }
}
